﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;
using System.Web;



namespace StudentWeb.Api.Controllers
{
    public class ProductMoController : ApiController
    {
        ProductMo _productBO = new ProductMo();
        #region 移动端产品信息 分页列表

        [HttpPost]
        [ValidToken]
        public IHttpActionResult GetProductMoList(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail, "参数错误"));
                var wc = _GetProductMoListWhereC(model.condidate);
                var result = _productBO.GetProductMoList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _GetProductMoListWhereC(dynamic qc)
        {
            var wc = "ProductID>0";
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.tabkey);
            if (!string.IsNullOrEmpty(tmp) && tmp != "all") { wc += $" and (ProductState={tmp})"; }
            tmp = ConvertHelper.DynamicToString(qc.ProductName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and ProductName like '%{tmp}%'"; }
            return wc;
        }
        #endregion

        #region 移动端产品信息 单条记录
     
        [HttpGet]
        [ValidToken]
        public IHttpActionResult GetProductMoInfo(string token, int productID)
        {
            try
            {
                var result = _productBO.GetProductMoInfo(productID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 移动端产品信息 图片上传

        public IHttpActionResult UploadFile()
        {
            var res = _productBO.UploadFile(HttpContext.Current.Request.Files);
            return Json(res);
        }

        #endregion


        #region 移动端产品信息 保存

        [HttpPost]
        [ValidToken]
        public IHttpActionResult SaveProductMoInfo(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var user = new TeacherBO().GetTeacherInfo(token);
                var result = _productBO.SaveProductMoInfo(model, user.TeacherDataID);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 移动端产品信息 状态

        [HttpPost]
        [ValidToken]
        public IHttpActionResult SetProductMoState(string token, [FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            int productID = model.id;
            int productState = model.state;
            try
            {
                var user = new TeacherBO().GetTeacherInfo(token);
                var result = _productBO.SetProductMoState(productID, productState);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 查询全部数据的内容
        public IHttpActionResult GetProductMoList()
        {
            try
            {
                var result = _productBO.GetProductMoList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion
    }
}
